package com.cdutetc.websocketTest.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cdutetc.websocketTest.handler.DataWebSocketHandler;

import lombok.RequiredArgsConstructor;

@Service
public class DataService {
    @Autowired
    private final DataWebSocketHandler dataWebSocketHandler;
    @Autowired
    public DataService(DataWebSocketHandler dataWebSocketHandler){
        this.dataWebSocketHandler=dataWebSocketHandler;
    }
    private final SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    private final Random random=new Random();
    //定时任务

    //实时
    public void sendTriggeredData(String eventName){
        String time=sdf.format(new Date());
        int value=random.nextInt(100);
        String data=String.format("事件数据-事件：%s,时间:%s,值:%d", eventName,time,value);
        dataWebSocketHandler.broadcastData(data);
    }
    
}
